package com.example.company

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.company.adapter.MerChantAdapter
import com.example.company.databinding.ActivityMerchantBinding
import com.example.company.entity.MerChant
import com.example.company.vm.CompanyViewModel
import com.example.image.glide.GlideImgLoader
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import kotlin.random.Random

@AndroidEntryPoint
@Route(path = "/company/MerchantActivity")
class MerchantActivity : BaseMvvmActivity<ActivityMerchantBinding>() {

    private val merChantsViewModel:CompanyViewModel by viewModels()
    val random = Random.nextInt(899999)+100000
    override fun initView() {
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.gotoApproval.setOnClickListener {
            ARouter.getInstance().build("/company/ApproveActivity").navigation()
        }
        mDataBinding.rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb1->{
                    mDataBinding.rb1.isChecked=true
                    mDataBinding.rb2.isChecked=false
                }
                R.id.rb2->{
                    mDataBinding.rb1.isChecked=false
                    mDataBinding.rb2.isChecked=true
                }
            }
            if (mDataBinding.rb1.isChecked){
                mDataBinding.ll1.visibility= View.VISIBLE
                mDataBinding.ll2.visibility=View.GONE
            }else{
                mDataBinding.ll1.visibility=View.GONE
                mDataBinding.ll2.visibility=View.VISIBLE
                merChantsViewModel.getMerChant()
            }
        }
        merChantsViewModel.mGetMerChantSuccess.observe(this){
            mDataBinding.rv.run {
                layoutManager = LinearLayoutManager(this@MerchantActivity)
                adapter = MerChantAdapter(it).apply {
                    notifyDataSetChanged()
                    setItemOnClickListener(object :MerChantAdapter.ItemOnClickListener{
                        override fun onClick(view: View, position: Int) {
                            ARouter.getInstance().build("/company/MerChantDetailActivity").withInt("merChantId",it[position].id).navigation()
                        }
                    })
                }
            }
            ToastUtils.showLong("显示成功")
        }
        merChantsViewModel.mError.observe(this){
            ToastUtils.showLong("显示失败")
        }
        mDataBinding.add.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        mDataBinding.submit.setOnClickListener {
            var merChantAdd = MerChant(
                applicationArea = mDataBinding.addEnterpriseApplicationArea.text.toString()
                    .toInt(),
                applicationNumber = "RZ" + random,
                contactMethod = 15053259809,
                documents = emptyList(),
                enterpriseName = mDataBinding.addEnterpriseName.text.toString(),
                enterpriseProfileId = 1,
                entryPersonnel = mDataBinding.addEnterprisePeopleCount.text.toString().toInt(),
                entryTime = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()),
                id = random,
                processingSteps = emptyList(),
                status = "新建",
                contactName = "冯小臭"
            )
            merChantsViewModel.postMerChant(merChantAdd)
            merChantsViewModel.mMerChantSuccess.observe(this){
                ToastUtils.showLong("添加成功")
                merChantsViewModel.getMerChant()
            }
        }
        merChantsViewModel.mError.observe(this){
            ToastUtils.showLong(it)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==200&&resultCode== RESULT_OK){
            val bean=data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideImgLoader.loadImage(this,bean.path,mDataBinding.add)
        }
    }
    override fun getLayoutId(): Int = R.layout.activity_merchant
}